package jk.spider.util.seed.impl;

import java.io.File;
import java.util.List;

import jk.spider.core.SpiderController;
import jk.spider.util.SpiderUtil;
import jk.spider.util.config.ConfigConstants;
import jk.spider.util.config.PropertySet;
import jk.spider.util.seed.Seed;

import org.apache.log4j.Logger;

public class PageSeed extends Seed {
	private static final Logger log = Logger.getLogger(PageSeed.class);
	public PageSeed(SpiderController controller, PropertySet props) {
		super(controller, props);
	}

	@Override
	public void dealSeed() {
		int count = 0;
		SpiderUtil util = new SpiderUtil();
		String filePath = props.getString(ConfigConstants.SEED_FILE, "");
		if(!SpiderUtil.isStringNull(filePath) && !filePath.equals("this")) {
			filePath = System.getProperty("user.dir")+ File.separator + "config" + File.separator + filePath;
			List<String> seedList = util.readFile(filePath);
			for(String strSeed : seedList) {
				this.inSeed(strSeed);
				count ++;
			}
		} 
		log.info("Loaded Seed " + count);
	}
	
	protected void inSeed(String strSeed) {
		String sdInfo [] = strSeed.split("\\|");
		if(sdInfo.length > 0) {
			String sdUrl = sdInfo[0];
			String site = sdInfo[1];
			int city = Integer.parseInt(sdInfo[2]);
			String type = sdInfo[3];
			
			if(sdInfo.length > 4) {
				int count = Integer.parseInt(sdInfo[4]);
//				sdUrl = sdUrl.replaceAll("PPP", count);
				controller.getStorage().getResourceDAO().createSeed(sdUrl, site, city, type, "seed");
			}
			
			
			controller.getStorage().getResourceDAO().createSeed(sdUrl, site, city, type, "seed");
		}
	}

}
